package com.why.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.baomidou.mybatisplus.extension.api.R;
import com.why.commonresults.Result;
import com.why.service.VodService;
import com.why.utils.AliyunVodSDKUtils;
import com.why.utils.ConstantPropertiesUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @program: guli_parent
 * @description:
 * @author: @why
 * @create: 2021-02-06 14:28
 **/
@RestController
@RequestMapping("/eduvod/video")
@CrossOrigin
public class VodController {

    @Autowired
    VodService service;

    @PostMapping("/uploadVideo")
    public Result uploadVideo(MultipartFile videoFile) {

        String videoId = service.uploadToAliyun(videoFile);
        return Result.ok().data("videoId", videoId);
    }

    /**
     * 删除单个视频
     *
     * @param id
     * @return
     */

    @DeleteMapping("/deleteVideo/{id}")
    public Result deleteVideo(@PathVariable String id) {
        service.deleteVideoById(id);
        return Result.ok();

    }

    /**
     * 删除多个视频
     */

    @DeleteMapping("/deleteVideos")
    public Result deleteVideos(@RequestParam("videoIds") List<String> videoIds) {
        if (videoIds.size() > 0) {
            service.deleteVideoByCourseId(videoIds);
        }
        return Result.ok();

    }


    @GetMapping("get-play-auth/{videoId}")
    public Result getVideoPlayAuth(@PathVariable("videoId") String videoId) throws
            Exception {
//获取阿里云存储相关常量
        String accessKeyId = ConstantPropertiesUtil.ACCESS_KEY_ID;
        String accessKeySecret = ConstantPropertiesUtil.ACCESS_KEY_SECRET;
//初始化
        DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(accessKeyId,
                accessKeySecret);
//请求
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        request.setVideoId(videoId);
//响应
        GetVideoPlayAuthResponse response = client.getAcsResponse(request);
//得到播放凭证
        String playAuth = response.getPlayAuth();
//返回结果
        return Result.ok().message("获取凭证成功").data("playAuth", playAuth);

    }


}
